import data_rdb from '@ohos.data.rdb';
import featureAbility from '@ohos.ability.featureAbility'
const STORE_CONFIG = { name: "mySQL.db" }
let context = featureAbility.getContext()
const SQL_CREATE_TABLE_article = "" +
"CREATE TABLE IF NOT EXISTS article " +
"(" +
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
"title TEXT, " +
"cover TEXT, " +
"time TEXT" +
");"

const SQL_CREATE_TABLE_content = "CREATE TABLE IF NOT EXISTS content " +
"(" +
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," +
"content_type TEXT," +
"content_data TEXT," +
"content_index INTEGER," +
"belong_article INTEGER," +
"CONSTRAINT belong FOREIGN KEY (belong_article) REFERENCES article (id) ON DELETE CASCADE" +
");"


export default {
    onInitialized(abilityInfo) {
        console.info('DataAbility onInitialized');
        let promise = data_rdb.getRdbStore(context, STORE_CONFIG, 1);
        promise.then((rdbStore) => {
            console.log("Get RdbStore DataAbility successfully.")
            rdbStore.executeSql(SQL_CREATE_TABLE_article,[],(err,data)=>{
                if(err){
                    console.log("建表article失败")
                    return
                }
                console.log("建表article成功")
            })
            rdbStore.executeSql(SQL_CREATE_TABLE_content,[],(err,data)=>{
                if(err){
                    console.log("建表content失败")
                    return
                }
                console.log("建表content成功")
            })
            this.myRdbStore = rdbStore
        }).catch((err) => {
            console.log("Get RdbStore failed, err: " + err)
        })
    }
};